package filePackets;
import java.io.*; 
import java.net.*; 


public class Sender 
{ 

/** 
* Send an object through a datagram socket. 
* @param o The object to send. 
* @param dSock The socket the object should be sent trough. 
* @param address Address of the reciepent 
* @param port The port of the reciepent. 
*/ 

    public void WriteObjectTo(Object o, DatagramSocket dSock, InetAddress address, int port) throws Exception 
    {

    	ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000); 
    	ObjectOutputStream os = new ObjectOutputStream(new BufferedOutputStream(byteStream)); 

    	os.flush(); 
    	os.writeObject(o); 
    	os.flush(); 

    	byte[] sendBuf = byteStream.toByteArray(); 
    	DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length, address, port); 
    	int byteCount = packet.getLength(); 
    	dSock.send(packet); 
    	os.close(); 

    } 

} 